package com.project.service.impl;

import com.project.bean.FacilityBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IFacilityService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class FacilityServiceImpl extends BaseService implements IFacilityService {
    @Override
    public void addFacility(FacilityBean facilityBean, int roomId) {
        SqlSession session = this.getSession();
        IFacilityMapper mapper = session.getMapper(IFacilityMapper.class);
        IRoomMapper rMapper = session.getMapper(IRoomMapper.class);
        mapper.addFacility(facilityBean);
        rMapper.updateState(roomId,"设施损坏");
        session.commit();
        session.close();
    }

    @Override
    public void updateState(int id, int roomId) {
        SqlSession session = this.getSession();
        IFacilityMapper fMapper = session.getMapper(IFacilityMapper.class);
        IRoomMapper rMapper = session.getMapper(IRoomMapper.class);
        fMapper.updateState(id,"已解决");
        if (fMapper.countByRoom(id,"未解决")==0){
            rMapper.updateState(id,"正常");
        }
    }

    @Override
    public List<FacilityBean> findAll(int roomId) {
        SqlSession session = this.getSession();
        IFacilityMapper fMapper = session.getMapper(IFacilityMapper.class);
        List<FacilityBean> list = fMapper.findAll(roomId);
        session.close();
        return list;
    }
}
